package com.powernode.dataaccessobject.dao;

import com.powernode.commons.util.DbUtils;
import com.powernode.dataaccessobject.entity.Employee;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("all")
/**
 * @Author Arrebol
 * @Date 2024/8/3 11:28
 * @Project jdbc
 * @Description：
 * 完成员工表t_employee中数据的增删改查
 * 增删改查操作简称CRUD操作（create read update delete）
 * dao不负责任何业务逻辑的处理，只负责CRUD操作
 * dao时JavaEE设计模式之一，dao中方法名也有规范：
 * 一般都是以insert delete update select开头
 */
public class EmployeeDao extends BaseDao{

    /**
     * 新增员工
     * @param employee  员工数据
     * @return          新增记录条数
     */
    public int insert(Employee employee) throws SQLException {
        /*Connection conn= DbUtils.getConnection();
        String insertSql="insert into t_employee (name, job, hiredate, salary, address) VALUES (?,?,?,?,?)";
        PreparedStatement ps=conn.prepareStatement(insertSql);
        ps.setString(1,employee.getName());
        ps.setString(2,employee.getJob());
        ps.setString(3,employee.getHireDate());
        ps.setDouble(4,employee.getSalary());
        ps.setString(5,employee.getAddress());
        int count = ps.executeUpdate();
        DbUtils.close(null,ps,conn);
        return count;*/

        String insertSql="insert into t_employee (name, job, hiredate, salary, address) VALUES (?,?,?,?,?)";
        int count = super.executeUpdate(insertSql, employee.getName(), employee.getJob(), employee.getHireDate(), employee.getSalary(),
                employee.getAddress());
        return count;
    }

    /**
     * 根据员工id删除员工信息
     * @param id        员工id
     * @return          返回1表示删除成功，否则删除失败
     */
    public int deleteByEmployeeId(Long id) throws SQLException {
        /*Connection conn = DbUtils.getConnection();
        String deleteSql="delete from t_employee where id = ?";
        PreparedStatement ps=conn.prepareStatement(deleteSql);
        ps.setLong(1,id);
        int count = ps.executeUpdate();
        DbUtils.close(null,ps,conn);
        return count;*/

        String deleteSql="delete from t_employee where id = ?";
        int count = super.executeUpdate(deleteSql, id);
        return count;
    }

    /**
     * 根据员工id修改员工信息
     * @param employee  新的员工信息
     * @return          返回1表示修改成功，否则修改失败
     */
    public int update(Employee employee) throws SQLException {
        /*Connection conn= DbUtils.getConnection();
        String updateSql="update t_employee set name = ?, job = ?, hiredate = ?,salary = ?,address = ? where id = ?";
        PreparedStatement ps=conn.prepareStatement(updateSql);
        ps.setString(1,employee.getName());
        ps.setString(2,employee.getJob());
        ps.setString(3,employee.getHireDate());
        ps.setDouble(4,employee.getSalary());
        ps.setString(5,employee.getAddress());
        ps.setLong(6,employee.getId());
        int count = ps.executeUpdate();
        DbUtils.close(null,ps,conn);
        return count;*/
        String updateSql="update t_employee set name = ?, job = ?, hiredate = ?,salary = ?,address = ? where id = ?";
        int count = super.executeUpdate(updateSql, employee.getName(), employee.getJob(), employee.getHireDate(), employee.getSalary(),
                employee.getAddress(), employee.getId());
        return count;
    }

    /**
     * 根据id查询员工信息
     * @param id        员工id
     * @return          此id对应的员工信息
     */
    public Employee selectById(Long id) throws SQLException {
        /*Employee answer=new Employee();
        Connection conn=DbUtils.getConnection();
        String selectSql="select id, name, job, hiredate, salary, address from t_employee where id = ?";
        PreparedStatement ps = conn.prepareStatement(selectSql);
        ps.setLong(1,id);
        ResultSet rs = ps.executeQuery();
        boolean status = false;
        while (rs.next()) {
            System.out.println("查询到的员工信息如下：");
            answer.setId(rs.getLong("id"));
            answer.setName(rs.getString("name"));
            answer.setJob(rs.getString("job"));
            answer.setHireDate(rs.getString("hiredate"));
            answer.setSalary(rs.getDouble("salary"));
            answer.setAddress(rs.getString("address"));
            status = true;
        }
        DbUtils.close(rs,ps,conn);
        if(!status){
            System.out.println("当前系统中没有此员工的信息！");
            return null;
        }
        return answer;*/
        String selectSql="select id, name, job, hiredate, salary, address from t_employee where id = ?";
        Employee answer = null;
        try {
            answer = queryOne(selectSql, Employee.class, id);
            if(answer==null){
                System.out.println("当前员工系统中不存在此id对应的员工信息");
            }
            return answer;
        } catch (Exception e){
            e.printStackTrace();
        }
        return answer;
    }

    /**
     * 获取所有员工信息
     * @return          员工列表
     */
    public List<Employee> selectAll() throws SQLException {
        /*List<Employee> employeeList = new ArrayList<>();
        Connection conn=DbUtils.getConnection();
        String selectSql="select * from t_employee";
        PreparedStatement ps = conn.prepareStatement(selectSql);
        ResultSet rs = ps.executeQuery();
        boolean status = false;
        while (rs.next()) {
            Employee employee=new Employee();
            employee.setId(rs.getLong("id"));
            employee.setName(rs.getString("name"));
            employee.setJob(rs.getString("job"));
            employee.setHireDate(rs.getString("hiredate"));
            employee.setSalary(rs.getDouble("salary"));
            employee.setAddress(rs.getString("address"));
            assert employeeList != null;
            employeeList.add(employee);
            if(!status){
                status = true;
            }
        }
        if(!status){
            System.out.println("当前员工管理系统中没有存储员工信息，请先向系统中录入员工信息！");
        }
        return employeeList;*/
        String selectSql="select * from t_employee";
        List<Employee> employees = null;
        try {
            employees = super.executeQuery(selectSql, Employee.class);
            if(employees==null || employees.size()==0){
                System.out.println("当前员工系统中还没有存放员工信息，请先录入员工信息！");
            }
        } catch (Exception e){
            e.printStackTrace();
        }
        return employees;
    }
}
